<?php

namespace App\HttpController;

use App\Service\MessageService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\Mysqli\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="通信系统")
 */
class Message extends StaffController
{
    /**
     * @ApiDescription(value="获取信息列表")
     * @Api(name="获取信息列表", path="/message/getMessageList", version="1.0.0")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     */
    public function getMessageList(): bool
    {
        $res = MessageService::getInstance()->getMessageList($this->sid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="获取好友消息详情")
     * @Api(name="获取好友消息详情", path="/message/getChat", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="sid", required="", notEmpty="", description="好友sid", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws Throwable
     * @throws Exception
     */
    public function getChat($sid, $limit, $page): bool
    {
        $res = MessageService::getInstance()->getChat($this->sid, $sid, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @ApiDescription(value="删除好友信息")
     * @Api(name="删除好友信息", path="/message/remove", version="1.0.0")
     * @Method(allow={POST})
     * @Param(name="fid", required="", notEmpty="", description="好友sid", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\ORM\Exception\Exception
     */
    public function remove($fid): bool
    {
        $res = MessageService::getInstance()->remove($this->sid, $fid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}